{$CLEO .cs}
{$USE CLEO+}
{$USE newOpcodes}
{$USE ini}

0000: NOP 

script_name "PoliceCar"

if
    0AA2: 31@ = load_dynamic_library "CLEO+.cleo"
then
    if
        0AA4: 30@ = get_dynamic_library_procedure "GetCleoPlusVersion" library 31@
    then
        0AA7: call_function_return 30@ num_params 0 pop 0 29@
        if
            not 29@ >= 0x1020000
        then
            0AD1: show_formatted_text_highpriority "Cleo+ 1.2.0 (or higher) version is required, the Busy Pedestrians mod is disabled" time 15000
            0AA3: free_dynamic_library 31@
            0A93: terminate_this_custom_script
        end
        0AA3: free_dynamic_library 31@
    end
else
    0AD1: show_formatted_text_highpriority "Cleo+ 1.2.0 (or higher) version is required, the Busy Pedestrians mod is disabled" time 15000
    0AA3: free_dynamic_library 31@
    0A93: terminate_this_custom_script
end

0AF0: 0@ = read_int_from_ini_file "cleo\Busy Pedestrians.ini" section "Settings" key "Police Car Event"
if
    0@ == 0
then
    0A93: terminate_this_custom_script
end

0AF0: 29@ = read_int_from_ini_file "cleo\Busy Pedestrians.ini" section "Police Car Event" key "TimePause"
29@ *= 1000
0AF0: 30@ = read_int_from_ini_file "cleo\Busy Pedestrians.ini" section "Police Car Event" key "TimePause2"
30@ *= 1000

wait 5000

:PoliceCar
wait 50
if
   Player.Defined($PLAYER_CHAR) 
jf @PoliceCar
if and
    $ONMISSION == 0
    $ACTIVE_INTERIOR == 0
    TIMERA < 3000
    810F:   not player $PLAYER_CHAR wanted_level > 0
jf @PoliceCar_Boot
    04C4: store_coords_to 1@ 2@ 3@ from_actor $PLAYER_ACTOR with_offset 0.0 0.0 0.0
    0AE2: 0@ = random_vehicle_near_point 1@ 2@ 3@ in_radius 40.0 find_next 1 pass_wrecked 1
jf @PoliceCar
    046C: 4@ = car 0@ drive
if
    not 4@ == -1
jf @PoliceCar
    089F: get_actor 4@ pedtype_to 3@
    0E12: get_vehicle 0@ subclass_to 1@
if and
    0431: car 0@ passenger_seat_free 1
    0431: car 0@ passenger_seat_free 2
    not is_car_script_controlled 0@
    not is_char_script_controlled 4@
    1@ == 0
    3@ == 6
    not locate_char_distance_to_car $PLAYER_ACTOR car 0@ radius 10.0
jf @PoliceCar
    04C4: store_coords_to 1@ 2@ 3@ from_actor 4@ with_offset 0.0 0.0 0.0
    get_random_char_in_sphere_no_save_recursive 5@ = random_char_near_point 1@ 2@ 3@ in_radius 25.0 find_next 1 pass_deads 1
jf @PoliceCar
if and
    not is_char_script_controlled 5@
    80DF:   not actor 5@ driving
    82F2:   not actor 5@ model == 71
    82F2:   not actor 5@ model == 153
    82F2:   not actor 5@ model == 260
    82F2:   not actor 5@ model == 16
    82F2:   not actor 5@ model == 27
jf @PoliceCar
    089F: get_actor 5@ pedtype_to 1@
if or
    1@ == 4
    1@ == 5
jf @PoliceCar
    0397: enable_car 0@ siren 1
    04C4: store_coords_to 1@ 2@ 3@ from_actor 5@ with_offset 0.0 1.0 0.0
    00A7: car 0@ drive_to 1@ 2@ 3@
    05DC: AS_actor 5@ run_away_from_point 1@ 2@ 3@ stop_at_radius 50.0 timelimit 15000
    0E0B: mark_char_as_needed 4@
    0E0B: mark_char_as_needed 5@

10@ = -1

:PoliceCar_3_3
wait 0
if and
056D:   actor 5@ defined
056D:   actor 4@ defined
056E:   car 0@ defined
jf @PoliceCar_Exit
080E: get_actor 4@ ped_event_to 1@
if and
not 1@ == 9 // ped damaged
not actor.Dead(4@)
81F4:   not car 0@ flipped
8495:   not car 0@ burning
0EE4: locate_char_distance_to_char $PLAYER_ACTOR char 4@ radius 50.0
jf @PoliceCar_Exit
04C4: store_coords_to 1@ 2@ 3@ from_actor 5@ with_offset 0.0 1.0 0.0
00A7: car 0@ drive_to 1@ 2@ 3@
if 
0EE5: locate_char_distance_to_car 5@ car 0@ radius 10.0
jf @PoliceCar_3_3

model.Load(346)
038B: load_requested_models

10@ = -1
if
    8431: not car 0@ passenger_seat_free 0
then
    0432: 10@ = get_actor_handle_from_car 0@ passenger_seat 0
    if
        10@ > 0
    then
        0615: define_AS_pack_begin 1@
        05CD: AS_actor -1 exit_car 0@
        0635: AS_actor -1 aim_at_actor 5@ -1 ms
        0616: define_AS_pack_end 1@
        0618: assign_actor 10@ to_AS_pack 1@
        061B: remove_references_to_AS_pack 1@
        01B2: give_actor 10@ weapon 22 ammo 60 // Load the weapon model before using this
    end
end

0615: define_AS_pack_begin 1@
05CD: AS_actor -1 exit_car 0@ 
0635: AS_actor -1 aim_at_actor 5@ -1 ms  
0616: define_AS_pack_end 1@ 
0618: assign_actor 4@ to_AS_pack 1@ 
061B: remove_references_to_AS_pack 1@ 

01B2: give_actor 4@ weapon 22 ammo 60 // Load the weapon model before using this  
model.Destroy(346)

060A: create_decision_maker_type 0 store_to 1@ // decision\allowed\m_.ped files
060B: set_actor 5@ decision_maker_to 1@

0407: store_coords_to 1@ 2@ 3@ from_car 0@ with_offset 0.0 0.0 0.0
04C4: store_coords_to 11@ 22@ 23@ from_actor 5@ with_offset 0.0 0.0 0.0
0E27: get_angle_from_two_coords 1@ 2@ and 11@ 22@ to 1@
000B: 1@ += 180.0
0173: set_actor 5@ Z_angle_to 1@
0812: AS_actor 5@ perform_animation "HANDSUP" IFP "PED" framedelta 4.0 loopA 0 lockX 0 lockY 0 lockF 1 time -1  // versionB 
0ABF: set_car 0@ engine_state_to 1
0006: TIMERA = 0
31@ = 1
8@ = 0
9@ = 0.0

:PoliceCar_CHeck
wait 0 
if and
056D:   actor 4@ defined
056D:   actor 5@ defined
056E:   car 0@ defined
jf @PoliceCar_Exit
080E: get_actor 4@ ped_event_to 1@
if and
not 1@ == 9 // ped damaged
not actor.Dead(4@)
81F4:   not car 0@ flipped 
82BF:   not car 0@ sunk 
8495:   not car 0@ burning
0EE5: locate_char_distance_to_car 4@ car 0@ radius 10.0
jf @PoliceCar_Exit
if
    actor.Dead(5@)
then
    01C2: remove_references_to_actor 4@ // Like turning an actor into a random pedestrian
    if 056D:   actor 4@ defined
    then 05D2: AS_actor 4@ run_to_and_hijack_car 0@ max_search_radius 10.0 traffic_behavior 5
    end
    4@ = -1
    
    if
        056D:   actor 10@ defined
    then
        05CA: AS_actor 10@ enter_car 0@ passenger_seat 0 time -1
        10@ = -1
    end
    
    jump @PoliceCar_Exit
end

    if
        31@ == 4
    then
        jump @PoliceCar_CHeck
    end

    if
        31@ == 1
    then
        jump @PoliceCar_4
    end

    if
        31@ == 2
    then
        jump @PoliceCar_5
    end

    if
        31@ == 3
    then
        jump @PoliceCar_6
    end

:PoliceCar_4
if
0019:   TIMERA > 4000
then
9@ += 0.05
08A6: set_car 0@ door 5 rotation_to 9@
end

if
0019:   TIMERA > 6000
jf @PoliceCar_CHeck
0657: car 0@ open_door 5 
01C2: remove_references_to_actor 5@ // Like turning an actor into a random pedestrian
060A: create_decision_maker_type 0 store_to 1@ // decision\allowed\m_.ped files
060B: set_actor 5@ decision_maker_to 1@
05CA: AS_actor 5@ enter_car 0@ passenger_seat 2 time -1
31@ = 2
0006: TIMERA = 0
jump @PoliceCar_CHeck

:PoliceCar_5
if
00DB:   actor 5@ in_car 0@
jf @PoliceCar_5_2
0ABF: set_car 0@ engine_state_to 1
020A: set_car 0@ door_status_to 1
05D2: AS_actor 4@ run_to_and_hijack_car 0@ max_search_radius 10.0 traffic_behavior 5
if
056D:   actor 10@ defined
then
05CA: AS_actor 10@ enter_car 0@ passenger_seat 0 time -1
end
31@ = 3
jump @PoliceCar_CHeck

:PoliceCar_5_2
if or
not locate_char_distance_to_char 4@ char 5@ radius 35.0
timera > 20000
jf @PoliceCar_CHeck
07A5: AS_actor 4@ attack_actor 5@ time -1
if
056D:   actor 10@ defined
then
07A5: AS_actor 10@ attack_actor 5@ time -1
end
31@ = 4
jump @PoliceCar_CHeck

:PoliceCar_6
if
00DB:   actor 4@ in_car 0@
jf @PoliceCar_CHeck
0397: enable_car 0@ siren 0
wait 5000
jump @PoliceCar_Exit


:PoliceCar_Exit
if 
056D:   actor 4@ defined
then
if and
80DF:   not actor 4@ driving
not actor.Dead(4@)
then
0792: disembark_instantly_actor 4@
end
01C2: remove_references_to_actor 4@ // Like turning an actor into a random pedestrian
end

if 
056D:   actor 5@ defined
then
if
056E:   car 0@ defined
then
if
actor.Dead(5@)
then
01C2: remove_references_to_actor 5@ // Like turning an actor into a random pedestrian
end
if
80DB:   not actor 5@ in_car 0@
then
01C2: remove_references_to_actor 5@ // Like turning an actor into a random pedestrian
end
else
01C2: remove_references_to_actor 5@ // Like turning an actor into a random pedestrian
end
end

if 
056D:   actor 10@ defined
then
if and
80DF:   not actor 10@ driving
not actor.Dead(10@)
then
0792: disembark_instantly_actor 10@
end
end

:PoliceCar_Time
wait 29@
0006: TIMERA = 0
jump @PoliceCar


// boot m4


:PoliceCar_boot
if and
    $ACTIVE_INTERIOR == 0
    TIMERA < 20000
    810F:   not player $PLAYER_CHAR wanted_level > 3
jf @PoliceCar_Time_2
04C4: store_coords_to 1@ 2@ 3@ from_actor $PLAYER_ACTOR with_offset 0.0 0.0 0.0
0AE1: 4@ = random_char_near_point 1@ 2@ 3@ in_radius 40.0 find_next 1 pass_deads 1
jf @PoliceCar
0EFF: get_char_simplest_active_task 4@ id_to 1@ pointer_to 2@
089F: get_actor 4@ pedtype_to 3@
if and
1@ == 1020
3@ == 6
80DF:   not actor 4@ driving
jf @PoliceCar
04C4: store_coords_to 1@ 2@ 3@ from_actor 4@ with_offset 0.0 0.0 0.0
0AE2: 0@ = random_vehicle_near_point 1@ 2@ 3@ in_radius 15.0 find_next 1 pass_wrecked 1
jf @PoliceCar
046C: 2@ = car 0@ drive
if
2@ == -1
jf @PoliceCar
if or
0137:   car 0@ model == 596
0137:   car 0@ model == 597
0137:   car 0@ model == 598
0137:   car 0@ model == 599
jf @PoliceCar
0A97: 10@ = car 0@ struct
0A8E: 11@ = 10@ + 0x18
0A8D: 11@ = read_memory 11@ size 4 virtual_protect 0
0AA7: call_function 0x4C5400 num_params 2 pop 2 "boot_dummy" 11@ 12@
if
12@ <> 0 // if "boot_dummy" find
jf @PoliceCar
0441: 1@ = car 0@ model
07E4: get_model 1@ dimensions_cornerA_to 2@ 8@ 2@ dimensions_cornerB_to 2@ 2@ 2@ // B
8@ -= 0.4
0407: store_coords_to 21@ 22@ 23@ from_car 0@ with_offset 0.0 8@ 0.5
0006: TIMERA = 0
31@ = 1

:Police_Anim
wait 0
04ED: load_animation  "PedEventsMod"
if
04EE:   animation "PedEventsMod" loaded
jf @Police_Anim

:Police_Check
wait 0
if and
056D:   actor 4@ defined
056E:   car 0@ defined
jf @Police_Exit
if and
not actor.Dead(4@)
81F4:   not car 0@ flipped
8495:   not car 0@ burning
jf @Police_Check
0A97: 10@ = car 0@ struct
0A8E: 11@ = 10@ + 0x18
0A8D: 11@ = read_memory 11@ size 4 virtual_protect 0
0AA7: call_function 0x4C5400 num_params 2 pop 2 "boot_dummy" 11@ 12@
if
12@ <> 0 // if "boot_dummy" find
jf @Police_Exit

if
31@ == 1
then
jump @Police_Point
end

if
31@ == 2
then
jump @Police_Point_2
end

if
31@ == 3
then
jump @Police_Open
end


:Police_Point
0407: store_coords_to 21@ 22@ 23@ from_car 0@ with_offset 0.0 8@ 0.5
if and
82CA:   not car 0@ bounding_sphere_visible
82CB:   not actor 4@ bounding_sphere_visible
jf @Police_CHeck
00A1: put_actor 4@ at 21@ 22@ 23@
0E24: fix_char 4@ ground 1 brightness 1 and_fade_in 0
0575: set_actor 4@ pinned_position 1
31@ = 2

:Police_Point_2
if
02CB:   actor 4@ bounding_sphere_visible
jf @Police_Check
0575: set_actor 4@ pinned_position 0
0174: 6@ = car 0@ Z_angle
0173: set_actor 4@ Z_angle_to 6@
0812: AS_actor 4@ perform_animation "Boot_Open" IFP "PedEventsMod" framedelta 2.0 loopA 0 lockX 0 lockY 0 lockF 0 time -1  // versionB
31@ = 3
0006: TIMERA = 0

:Police_Open
0613: 1@ = actor 4@ animation "Boot_Open" time
08A6: set_car 0@ door 1 rotation_to 1@
if
0019:   TIMERA > 500
jf @Police_Check
0812: AS_actor 4@ perform_animation "Boot_Idles" IFP "PedEventsMod" framedelta 2.0 loopA 0 lockX 0 lockY 0 lockF 0 time 1000  // versionB
model.Load(356)
038B: load_requested_models
01B2: give_actor 4@ weapon 31 ammo 999 // Load the weapon model before using this
model.Destroy(356)


:Police_Check_2
wait 0
if
056D:   actor 4@ defined
jf @Police_Exit
01B9: set_actor 4@ armed_weapon_to 31
if or
is_char_script_controlled 4@
actor.Dead(4@)
not locate_char_distance_to_char $PLAYER_ACTOR char 4@ radius 50.0
jf @Police_Check_2

:Police_Exit
04EF: release_animation "PedEventsMod"
jump @PoliceCar

:PoliceCar_Time_2
wait 30@
0006: TIMERA = 0
jump @PoliceCar

// By ArtemQa146